//
//  DAPersonalFileListTableViewCell.m
//  DAKnowledge
//
//  Created by 黄健宇 on 16/7/26.
//  Copyright © 2016年 lidongyang. All rights reserved.
//

#import "DAPersonalFileListTableViewCell.h"
#import "DAHeader.h"
#import "Utility.h"
#import "DAPersonalFileListModel.h"

@implementation DAPersonalFileListTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
    self.progress.tintColor = [UIColor supportGreenColor];
}

- (void)setUserFileModel:(DAPersonalFileListModel *)userFileModel{
    _userFileModel = userFileModel;
    
    NSString *file = [NSString stringWithFormat:@"file_%@.png",userFileModel.fileExtend];
    UIImage *image = [UIImage imageNamed:file];
    self.fileNameLabel.text = userFileModel.fileName;
    self.fileImageView.image = image;
    self.fileIntroduction.text = userFileModel.fileType;
    self.fileIntroduction.textColor = [UIColor colorWithHexString:timeColor];
    //进度条处理
    if(userFileModel.isDownloading == NO){
        self.progress.hidden = YES;
        self.progress.progress = 0;
    }else{
        self.progress.hidden = NO;
    }
    //
    //    //是否已下载
    //    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    //    NSString *documentsDirectory = [paths objectAtIndex:0];
    //    //NSLog(@"文件保存路径%@",documentsDirectory);
    //    BOOL isExist = [self isFileExist:[NSString stringWithFormat:@"%@.%@",userFileModel.filefolderName,userFileModel.fileExtend] inPath:documentsDirectory];
    //    if (isExist) {
    //        self.downLoadCompleteImage.image = [UIImage imageNamed:@"complete@3x"];
    //    }
    
    if (userFileModel.isDownloadingSuccessful == YES) {
        self.downLoadCompleteImage.image = [UIImage imageNamed:@"complete@3x"];
    }else{
        self.downLoadCompleteImage.image = nil;
    }
    
    //处理文件大小
    int fileSize = [userFileModel.fileSize intValue];
    float fileSizeK = 0.0;
    float fileSizeM = 0.0;
    if (fileSize > 999 && fileSize < 999*1024) {
        fileSizeK = fileSize/1024.0;
        self.fileSize.text = [NSString stringWithFormat:@"%.2fK",fileSizeK];
    }else if (fileSize >= 999*1024 ){
        fileSizeM = fileSize/(1024.0*1024.0);
        self.fileSize.text = [NSString stringWithFormat:@"%.2fM",fileSizeM];
    }else{
        self.fileSize.text = [NSString stringWithFormat:@"%dB",fileSize];
    }
    self.fileSize.textColor = [UIColor colorWithHexString:timeColor];
    
    
}

#pragma mark - 判断沙盒里是否有这个文件
-(BOOL)isFileExist:(NSString *)fileName inPath:(NSString *)path{
    NSString *filePath = [path stringByAppendingPathComponent:fileName];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL result = [fileManager fileExistsAtPath:filePath];
    //  NSLog(@"该文件：%@",result?@"有！！":@"不存在");
    return result;
}


@end
